﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Threading;
using ExtendedEditor.Threading;
using System.Windows.Documents;
using PSLib.CodeFormating.Lexems;
using PSLib.CodeFormating.Adapters;

namespace ExtendedEditor.Painter
{
	[Serializable]
	public class BTPainterCommandRequest : BTCommandRequest
	{
		public BTPainterCommandRequest()
		{
			
		}
	}

	[Serializable]
	public class BTPainterCommandResponse : BTCommandResponse
	{
		public BTPainterCommandResponse(BTPainterCommandRequest request, LexemList lexemList)
		{
			this.Request = request;
			this.LexemList = lexemList;
		}
		public LexemList LexemList { get; private set; }
		public BTPainterCommandRequest Request { get; private set; }
	}

	public class TextBoxPainter : BackgroundThread
	{
		#region Dependency properties

			#region Owner property

			public static readonly DependencyProperty OwnerProperty = DependencyProperty.Register(
													"Owner",
													typeof(RichTextBox),
													typeof(TextBoxPainter),
													new FrameworkPropertyMetadata(null, OwnerValueDependencyChanged));

			private static void OwnerValueDependencyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
			{
				TextBoxPainter popup = target as TextBoxPainter;
				if (popup != null) popup.SetOwner(e.NewValue as RichTextBox, e.OldValue as RichTextBox);
			}

			private void SetOwner(RichTextBox newValue, RichTextBox oldValue)
			{
				if (newValue == oldValue) return;
				if (oldValue != null)
				{
					oldValue.TextChanged -= new TextChangedEventHandler(owner_TextChanged);
					//oldValue.PreviewKeyDown -= new System.Windows.Input.KeyEventHandler(owner_PreviewKeyDown);
				}

				if (newValue != null)
				{
					newValue.TextChanged += new TextChangedEventHandler(owner_TextChanged);
					//newValue.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(owner_PreviewKeyDown);
				}
			}


			public RichTextBox Owner
			{
				get { return GetValue(TextBoxPainter.OwnerProperty) as RichTextBox; }
				set { SetValue(TextBoxPainter.OwnerProperty, value); }
			}

			#endregion

		#endregion

		#region Constructors properties
		
		public TextBoxPainter()
		{
			index = new Dictionary<Guid, Paragraph>();
		}

		#endregion

		#region Background thread

		protected override void OnBTCommandRequest(BTCommandRequest command)
		{
			BTPainterCommandRequest request = command as BTPainterCommandRequest;

			//LexemList list = LexemParser.Parse(request.Text, LanguageAdapter.LuaAdaptor);
			//BTPainterCommandResponse response = new BTPainterCommandResponse(request, list);
			//Response(response);
		}

		protected override void OnBTCommandResponse(BTCommandResponse command)
		{
			BTPainterCommandResponse response = command as BTPainterCommandResponse;

			this.Owner.BeginChange();
			//index[response.Request.Paragraph].Inlines.Clear();
			//index[response.Request.Paragraph].Inlines.Add(response.Run);
			//response.Request.
			this.Owner.EndChange();
		}

		#endregion

		#region Event handlers

		private void owner_TextChanged(object sender, TextChangedEventArgs e)
		{
			//List<Paragraph> parList = new List<Paragraph>();
			//foreach (var item in e.Changes)
			//{
			//    TextPointer start = this.Owner.Document.ContentStart.GetPositionAtOffset(item.Offset);
			//    TextPointer end = this.Owner.Document.ContentStart.GetPositionAtOffset(item.Offset + item.AddedLength);
			//    parList.AddRange(this.Owner.Document.GetParagraphRange(start, end));
			//}
			
			//Enumerate all editable blocks
			//for (int i = 0; i < parList.Count; i++)
			//{
			//    BTPainterCommandRequest command = new BTPainterCommandRequest(parList[i]);
			//    if (!index.ContainsKey(command.Paragraph)) index.Add(command.Paragraph, parList[i]);
			//    EnqueueCommand(command);
			//}
		}

		#endregion

		Dictionary<Guid, Paragraph> index;
	}
}
